import pandas as pd
import requests
import math
import os
from tqdm import tqdm

def get_geocode(address, api_key):
    """
    调用高德地理编码API获取经纬度
    :param address: 地址字符串
    :param api_key: 高德API密钥
    :return: (经度, 纬度) 或 None
    """
    url = f"https://restapi.amap.com/v3/geocode/geo?address={address}&key={api_key}"
    try:
        response = requests.get(url, timeout=5)
        result = response.json()
        if result['status'] == '1' and result['geocodes']:
            location = result['geocodes'][0]['location']
            lon, lat = location.split(',')
            address_spyder = str(result['geocodes'][0]['formatted_address'])
            return float(lon), float(lat),address_spyder
    except Exception as e:
        print(f"请求失败: {address} - {e}")
    return None

